import axios from 'axios';
import JsonP from 'jsonp';
import { Modal, message } from 'antd/lib';

const ERR_RETURN_DURATION = 3; // 错误提示间隔时间

export default class Axios {
  static jsonp({ url }) {
    return new Promise((resolve, reject) => {
      JsonP(url, { param: 'callback' }, (err, res) => {
        if (res.status !== 'success') return reject(res.messsage);
        return resolve(res);
      });
    });
  }

  static apiPath = '这只是静态方法或者是静态变量';

  static ajax(options, method = 'get') {
    return new Promise((resolve, reject) => {
      axios({
        // baseURL: 'https://www.easy-mock.com/mock/5d2dcb9a934181050dd4ae65/api',
        url: options.url,
        method: method,
        timeout: 5000,
        params: (options && options.params) || '',
        headers: { ACJSESSIONID: sessionStorage.getItem('ACJSESSIONID') },
      })
        .then(response => {
          let res = response.data;
          if (response.status === 200) {
            resolve(res);
            // 判断后台返回的状态
            // if (res.resultCode === 'ok') return resolve(res);
            // Modal.info({ title: '提示', content: 'res.errCode 请求后台数据错误' });
            // reject(res);
          } else {
            reject(res);
          }
        })
        .catch(err => {
          Modal.info({ title: '提示', content: '网络请求错误' });
          reject(err);
        });
    });
  }

  static get(url, param) {
    return new Promise((resolve, reject) => {
      axios.get(url, param).then(res => {
        if (res.status === 200) {
          resolve(res);
        }
        if (res && res.status !== 200) {
          reject(res);
          message.error('请求的状态码不是200', ERR_RETURN_DURATION);
        }
      });
    }).catch(err => message.warning(err));
  }

  static post(url, param) {
    console.log(url);
    return new Promise((resolve, reject) => {
      axios.post(url + url, param).then(res => {
        if (res.status === 200) {
          message.info(res.data.errMessage);
          return resolve(res);
        }
        reject(res.data);
        return message.error('请求的状态码不是200');
      });
    });
  }
}
